<template>
  <div class="app-container">
    <el-table v-loading="loading" border :data="tableData">
      <el-table-column label="消息类型" align="center"  prop="repType" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wx_rep_type" :value="scope.row.repType"/>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="nickName" width="100"/>
      <el-table-column label="头像" align="center"  width="100">
        <template slot-scope="scope" >
          <el-avatar shape="square" :size="50"  :src="scope.row.headimgUrl"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="内容" align="left" prop="repContent" >
        <template  slot-scope="scope" >
          <div class="nick-name">{{scope.row.nickName}}</div>
          <div v-if="scope.row.repType == 'event' && scope.row.repEvent == 'subscribe'"><el-tag type="success" size="mini">关注</el-tag></div>
          <div v-if="scope.row.repType == 'event' && scope.row.repEvent == 'unsubscribe'"><el-tag type="danger" size="mini">取消关注</el-tag></div>
          <div v-if="scope.row.repType == 'event' && scope.row.repEvent == 'CLICK'"><el-tag size="mini">点击菜单</el-tag>：【{{scope.row.repName}}】</div>
          <div v-if="scope.row.repType == 'event' && scope.row.repEvent == 'VIEW'"><el-tag size="mini">点击菜单链接</el-tag>：【{{scope.row.repUrl}}】</div>
          <div v-if="scope.row.repType == 'event' && scope.row.repEvent == 'scancode_waitmsg'"><el-tag size="mini">扫码结果：</el-tag>：【{{scope.row.repContent}}】</div>
          <div v-if="scope.row.repType == 'text'">{{scope.row.repContent}}</div>
          <div v-if="scope.row.repType == 'image'">
            <a target="_blank" :href="scope.row.repUrl"><img :src="scope.row.repUrl" style="width: 100px"></a>
          </div>
          <div v-if="scope.row.repType == 'voice'">
            <WxVoicePlayer :objData="scope.row"></WxVoicePlayer>
          </div>
          <div v-if="scope.row.repType == 'video'">
            <WxVideoPlayer :objData="scope.row" style="margin-top: 40px"></WxVideoPlayer>
          </div>
          <div v-if="scope.row.repType == 'shortvideo'">
            <WxVideoPlayer :objData="scope.row" style="margin-top: 40px"></WxVideoPlayer>
          </div>
          <div v-if="scope.row.repType == 'link'"><el-tag size="mini">链接</el-tag>：<a :href="scope.row.repUrl" target="_blank">{{scope.row.repName}}</a></div>
        </template>
      </el-table-column>
      <el-table-column label="是否已读" align="center" prop="readFlag" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_enable" :value="scope.row.readFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作"  fixed="right" width="80" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-chat-line-round"
            @click="wxMsgDo(scope.row,scope.index)"
          >消息</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getPage"
    />
      <el-dialog title="用户消息" :visible.sync="dialogMsgVisible" width="40%">
        <WxMsg :wxUserId="wxUserId" v-if="dialogMsgVisible"></WxMsg>
      </el-dialog>
  </div>
</template>

<script>
  import { getMsgList,addObj, putObj} from '@/api/mp/msg'
  import WxMsg from '@/components/WxMp/WxMsg.vue'
  import WxVideoPlayer from '@/components/WxMp/VideoPlay.vue'
  import WxVoicePlayer from '@/components/WxMp/VoicePlay.vue'
  export default {
    name: 'wxmsg',
    components: {
      WxMsg,
      WxVideoPlayer,
      WxVoicePlayer
    },
    dicts: ['wx_rep_type','sys_is_enable'],
    data() {
      return {
        dialogMsgVisible: false,
        tableData: [],
        total: 0,
        queryParams: {
          pageNum: 1,
          pageSize: 20
        },
        paramsSearch:{},
        loading: false,
        wxUserId: ''
      }
    },
    created() {
      this.getPage();
    },
    mounted: function() { },
    computed: {
    },
    methods: {
      wxMsgDo(row){
        this.wxUserId = row.wxUserId
        this.dialogMsgVisible = true
        this.$set(row, 'readFlag', '1')
      },
      sortChange(val){
        let prop = val.prop ? val.prop.replace(/([A-Z])/g,"_$1").toLowerCase() : '';
        if(val.order=='ascending'){
          this.page.descs = []
          this.page.ascs = prop
        }else if(val.order=='descending'){
          this.page.ascs = []
          this.page.descs = prop
        }else{
          this.page.ascs = []
          this.page.descs = []
        }
        this.getPage(this.page)
      },
      getPage() {
        this.loading = true
        getMsgList(this.queryParams).then(response => {
          this.tableData = response.rows
          this.total = response.total;
          this.loading = false;
        }).catch(() => {
          this.loading = false
        })
      },
      /**
       * @title 数据更新
       * @param row 为当前的数据
       * @param index 为当前更新数据的行数
       * @param done 为表单关闭函数
       *
       **/
      handleUpdate: function(row, index, done, loading) {
        putObj(row).then(data => {
          this.$message({
            showClose: true,
            message: '修改成功',
            type: 'success'
          })
          done()
          this.getPage(this.page)
        }).catch(() => {
          loading()
        })
      },
      /**
       * @title 数据添加
       * @param row 为当前的数据
       * @param done 为表单关闭函数
       *
       **/
      handleSave: function(row, done, loading) {
        addObj(row).then(data => {
          this.$message({
            showClose: true,
            message: '添加成功',
            type: 'success'
          })
          done()
          this.getPage(this.page)
        }).catch(() => {
          loading()
        })
      },
      /**
       * 刷新回调
       */
      refreshChange(page) {
        this.getPage(page)
      }
    }
  }
</script>

<style lang="scss" scoped>
  .head-img{
    width: 50px;
  }
</style>
